package com.aliyun.oss.controller;

import com.aliyun.oss.pojo.*;
import com.aliyun.oss.service.ArticleCategoryService;
import com.aliyun.oss.service.ArticleManageService;
import com.aliyun.oss.service.UserInfoPicService;
import com.aliyun.oss.utils.AliyunOSSOperator;
import com.aliyun.oss.utils.Base64TranstrFile;
import com.aliyun.oss.utils.JwtUtil;
import com.aliyuncs.exceptions.ClientException;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.xml.transform.Result;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/my")
public class UserInfoPicController {
    @Autowired
    AliyunOSSOperator aliyunOSSOperator;
    @Autowired
    ArticleManageService articleManageService;
    @Autowired
    UserInfoPicService userInfoPicService;
    //    获取用户信息
    @Autowired
    ArticleCategoryService articleCategoryService;

    @GetMapping("userinfo")
    public ResultObj selectUserUserInfoPic(@RequestHeader("Authorization") String authHeader) {
        UserInfoPic userInfoPic = userInfoPicService.getUserUserInfoPic(authHeader);
        return ResultObj.Success(userInfoPic);
    }

    //      获取文章分类
    @GetMapping("/cate/list")
    public ResultObj selectAllArticleCategory(@RequestHeader("Authorization") String authHeader) {
        Claims claims = JwtUtil.analysisToken(authHeader);
        Integer id = (Integer) claims.get("id");
        List<ArticleCategory> articleCategories = articleCategoryService.selectAllArticleCategory(id);
        return ResultObj.Success(articleCategories);
    }

    @PostMapping("/cate/add")
    public ResultObj addArticleCategory(@RequestHeader("Authorization") String authHeader, @RequestBody ArticleCategory articleCategory) {
        articleCategoryService.addArticleCategory(authHeader, articleCategory);
        return ResultObj.Success();
    }

    @PutMapping("/cate/info")
    public ResultObj updateArticleCategory(@RequestHeader("Authorization") String authHeader, @RequestBody ArticleCategory articleCategory) {
        articleCategoryService.updateArticleCategory(authHeader, articleCategory);
        return ResultObj.Success();
    }

    @DeleteMapping("/cate/del")
    public ResultObj deleteArticleCategory(@RequestHeader("Authorization") String authHeader, @RequestParam("id") String id) {
        articleCategoryService.deleteArticleCategory(authHeader, id);
        return ResultObj.Success();
    }

    @GetMapping("/article/list")
    public ResultObj getArticleList(@RequestHeader("Authorization") String authHeader, ArticleListParams articleListParams) {
        List<ArticleInfo> articleInfos = articleManageService.getArticleList(authHeader, articleListParams);
        return ResultObj.Success(articleInfos);
    }

    @GetMapping("/article/allList")
    public ResultObj getAllArticleList(@RequestHeader("Authorization") String authHeader) {
        Integer total = articleManageService.getAllArticleList(authHeader);
        return ResultObj.Success(total);
    }

    @PostMapping("/article/add")
    public ResultObj addArticle(@RequestHeader("Authorization") String authHeader, @ModelAttribute ArticleContent articleContent) throws IOException, ClientException {
        MultipartFile coverImg = articleContent.getCoverImg();
//        检查文件是否为空
        if (coverImg == null || coverImg.isEmpty()) {
            articleManageService.addArticle(authHeader, articleContent);
        } else {
            String originalFilename = coverImg.getOriginalFilename();
            byte[] content = coverImg.getBytes();
            String upload = aliyunOSSOperator.upload(content, originalFilename);
            articleContent.setImgUrl(upload);
            articleManageService.addArticle(authHeader, articleContent);
        }
        return ResultObj.Success();
    }
    @GetMapping("/article/info")
    public ResultObj getArticleInfo(@RequestHeader("Authorization") String authHeader,Integer id){
        ArticleContent articleContent=articleManageService.getArticleInfo(authHeader,id);
        return ResultObj.Success(articleContent);
    }
    @PutMapping("/article/infos")
    public ResultObj updateArticleInfo(@RequestHeader("Authorization") String authHeader,@ModelAttribute ArticleContent articleContent) throws IOException, ClientException {
        articleManageService.updateArticleInfo(authHeader,articleContent);
        return ResultObj.Success();
    }
    @PutMapping("/userinfo")
    public ResultObj updateUserInfo(@RequestHeader("Authorization") String authHeader,@RequestBody UserInfoPic userInfoPic){
        userInfoPicService.updateUserInfo(authHeader,userInfoPic);
        return ResultObj.Success();
    }

    @PatchMapping("/update/avatar")
    public ResultObj updateUserAvatar(@RequestHeader("Authorization") String authHeader, @RequestBody Map<String,Object> base64){
        // 获取前端传递的base64字符串
        String base64Str = (String) base64.get("avatar");
        try{
//            提取文件字节数组
            byte[] content= Base64TranstrFile.extractContent(base64Str);
// 提取原始文件名（包含扩展名）
            String originalFilename = Base64TranstrFile.extractOriginalFilename(base64Str);
            String upload=aliyunOSSOperator.upload(content,originalFilename);
            userInfoPicService.updateUserAvatar(authHeader,upload);
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.Error();
        }
        return ResultObj.Success();
    }

    @PatchMapping("/updatepwd")
    public ResultObj updateUserPwd(@RequestHeader("Authorization") String authHeader,@RequestBody Map<String,Object> pwds){
        System.out.println("这是:"+pwds);
        userInfoPicService.updateUserPwd(authHeader,pwds);
        return ResultObj.Success();
    }
}
